/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.File;
import java.io.IOException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import generated.ItemType;
import generated.ShoppingCart;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 *
 * @author Administrator
 */
public class Process {

    public static void readDOM(File file, Vector itemList) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(file);
            XPathFactory xpf = XPathFactory.newInstance();
            XPath xpath = xpf.newXPath();
            String expression = "//shoppingCart/*";
            NodeList list = (NodeList) xpath.evaluate(expression, doc, XPathConstants.NODESET);
            for (int i = 0; i < list.getLength(); i++) {
                ItemType temp = new ItemType();
                Node node = list.item(i);
                String tmp; //temporary variable to save the data
                //fill data to item object
                tmp = node.getChildNodes().item(1).getTextContent();
                temp.setItemID(tmp);
                tmp = node.getChildNodes().item(3).getTextContent();
                temp.setItemModel(tmp);
                tmp = node.getChildNodes().item(5).getTextContent();
                temp.setItemDescription(tmp);
                tmp = node.getChildNodes().item(7).getTextContent();
                temp.setItemPrice(tmp);
                tmp = node.getChildNodes().item(9).getTextContent();
                temp.setItemImage(tmp);
                tmp = node.getChildNodes().item(11).getTextContent();
                temp.setItemQuantity(tmp);

                //add to the vector
                itemList.add(temp);
            }

        } catch (SAXParseException ex) {
        } catch (XPathExpressionException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SAXException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void writeToFile(File file, Vector itemList, ShoppingCart shop) {
        try {
            for (int i = 0; i < itemList.size(); i++) {
                ItemType item = (ItemType) itemList.elementAt(i);
                shop.getItem().add(item);
            }

            JAXBContext ctx = JAXBContext.newInstance(shop.getClass());
            Marshaller mar = ctx.createMarshaller();
            mar.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            mar.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            
            
            mar.marshal(shop, file);
        } catch (JAXBException ex) {
            Logger.getLogger(Process.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static void removeCart(String itemID, String itemQuantity, Vector itemList) {
        ItemType temp;
        for (int i = 0; i < itemList.size(); i++) {
            temp = (ItemType) itemList.elementAt(i);
            if (temp.getItemID().equals(itemID) && temp.getItemQuantity().equals(itemQuantity)) {
                itemList.removeElementAt(i);
                return;
            }
        }
    }
    
    public static void editCart(String itemID, String itemQuantity, Vector itemList, String itemNewQuantity) {
        ItemType temp;
        for (int i = 0; i < itemList.size(); i++) {
            temp = (ItemType) itemList.elementAt(i);
            if (temp.getItemID().equals(itemID) && temp.getItemQuantity().equals(itemQuantity)) {
                temp.setItemQuantity(itemNewQuantity);
                return;
            }
        }
    }
}
